{extend name="public:base"}
{block name="body"}
<body>
		<form class="layui-form layui-form-pane" action="">
			<div class="mainBox">
				<div class="main-container">
                   <div class="layui-row">
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                            <div class="layui-form-item">
                                <label class="layui-form-label">场地名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="place_title" value="{$info.place_title|default=''}" lay-verify="required" autocomplete="off"
                                        placeholder="请输入标题" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                            <div class="layui-form-item">
                                <label class="layui-form-label">场地地址</label>
                                <div class="layui-input-block">
                                    <input type="text" name="address" value="{$info.address|default=''}" lay-verify="required" autocomplete="off"
                                        placeholder="请输入地址" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                            <div class="layui-form-item">
                                <label class="layui-form-label">地图坐标</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="lnglat" value="{$info.lnglat|default=''}" lay-verify="required" autocomplete="off" class="layui-input" disabled>
                                </div>
                                <div class="layui-form-mid" style="padding:0px!important">
                                    <a href="JavaScript:;" title="点击获取坐标" class="pear-btn pear-btn-primary " id="getAddress"><i class="layui-icon layui-icon-search"></i></a>
                                </div>
                               
                            </div>
                        </div>
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                            <div id="mapContainer" style="min-height:400px"></div>
                        </div>
                   </div>
                </div>
            </div>
            <div class="bottom">
            <div class="button-container">
                <input type="hidden" name="id" value="{$info.id|default=''}">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="form-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
            </div>
		</form>
	</body>
{/block}
{block name="script"}
<script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=6787b252616c535173296dc527bc172a&plugin=AMap.Geocoder"></script>
<script>
    var map = new AMap.Map("mapContainer", {
        resizeEnable: true,
        zoom:12
    });
    
    var geocoder = new AMap.Geocoder({
      //城市设为北京，默认：“全国”
    });
    
    var marker = new AMap.Marker();
    {notempty name="info.lnglat"}
        marker.setPosition({$info.lnglat});
        map.add(marker);
        map.setFitView(marker);
    {/notempty}
    
    layui.use(['jquery','form'], function() {
        let form = layui.form;
        let $ = layui.jquery;
        let tinymce = layui.tinymce;
        map.on('click',function(e){
         $('input[name=lnglat]').val(e.lnglat);
         map.add(marker);
        marker.setPosition(e.lnglat);
    })
        $('#getAddress').on('click',function(){
            let address = $('input[name=address]').val();
            console.log(address);
            if(address == ''){
                layer.msg('请输入地址');
            }
            geocoder.getLocation(address, function(status, result) {
                if (status === 'complete'&&result.geocodes.length) {
                    var lnglat = result.geocodes[0].location
                    $('input[name=lnglat]').val(lnglat);
                    marker.setPosition(lnglat);
                    map.add(marker);
                    map.setFitView(marker);
                }else{
                    log.error('根据地址查询位置失败');
                }
            });
        })
        form.on('submit(form-save)', function(data) {
            data.field.content = edit.getContent();
            $.ajax({
                url: '{:url("edit")}',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function(result) {
                    if (result.code == 1) {
                        layer.msg('操作成功', {
                            icon: 1,
                            time: 1000
                        }, function() {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                            parent.layui.table.reload("table-list");
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            })
            return false;
        });
    })
</script>
{/block}
